<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Car extends AdminController{
	public $site_conf = null;

	function __construct(){
		parent::__construct();
        $this->siteConf = $this->db->get('site_config')->row();
		$this->load->model('MCar');
		$this->_validation = Array(
			array('field'=>'name', 'label'=>'电单车名称', 'rules'=>'required|max_length[64]')
			,array('field'=>'brand', 'label'=>'品牌', 'rules'=>'max_length[64]')
			,array('field'=>'group_id', 'label'=>'优惠活动组', 'rules'=>'')
			,array('field'=>'picture', 'label'=>'电单车图片', 'rules'=>'required')
			,array('field'=>'qr_code', 'label'=>'电单车小程序码', 'rules'=>'')
            ,array('field'=>'status', 'label'=>'状态', 'rules'=>'integer')
            ,array('field'=>'remark', 'label'=>'备注', 'rules'=>'max_length[255]')
			,array('field'=>'price', 'label'=>'价格/天', 'rules'=>'required')
            ,array('field'=>'last_return_time', 'label'=>'上次还车时间', 'rules'=>'')
            ,array('field'=>'create_time', 'label'=>'创建时间', 'rules'=>'')
            ,array('field'=>'update_time', 'label' => '更新时间', 'rules' => '')
		);
	}

	function index(){
		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('name', 'like');
		$this->formfilter->addFilter('brand','where');
		$this->formfilter->addFilter('status','where');
        if ($begintime = filterValue('return_time_begin')) {
            $this->formfilter->addFilter('return_time_begin', 'where', array('last_return_time >= ', strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = filterValue('return_time_end')) {
            $this->formfilter->addFilter('return_time_end', 'where', array('last_return_time <= ', strtotime($endtime . ' 23:59:59')));
        }
        $number_perpage = filterValue('shop_number_perpage');
        $total_number = $this->MCar->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list  = $this->MCar->getList($limit);
		$this->assign('list', $list);
		$this->layout();
	}

	function add(){
	    $obj = $this->MCar->createVo();
	    $obj->price = $this->siteConf->car_price_day;
	    $this->load->library('form_validation');
	    $this->load->library('QR',null,'code');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->db->trans_start();
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        $cid = $this->MCar->add($newObj);
	        if(!$cid){
	           $this->db->_trans_status = false;
	        }else{
                // 生成小程序码
                $url = $this->code->qr_code($cid);

                if (!$url){
                    $this->db->_trans_status = false;
                }
                $car = $this->MCar->getById($cid);
                $car->qr_code = $url;
                if (!$this->MCar->update($car)){
                    $this->db->_trans_status = false;
                }
	        }
	        $this->db->trans_complete();
	        if ($this->db->_trans_status !== false){
                successAndRedirect(l('add_success'));
            }else{
                errorAndRedirect(l('添加失败，请稍后重试'));
            }
	    }
	    $this->assign('obj', $obj);
	    $this->layout();
	}

	function edit($id){
	    if(!is_numeric($id)){
	        show_error(l('id_not_null'));
	    }
	    $obj = $this->MCar->getOne(Array('id'=>$id));

	    if(!$obj){
	        errorAndRedirect('未找到定义！');
	    }
	    $this->editObj = $obj;
	    $this->load->library('form_validation');
	    $this->form_validation->set_rules($this->_validation);
	    if($this->form_validation->run()==TRUE){
	        $this->load->helper('populate');
	        $newObj = populate((Object)((Array)$obj), $this->form_validation->post());
	        if ($newObj->status == 0){
	            $exist = $this->MCar->getOne(['id' => $id, 'status <> ' => 0]);
	            if ($exist){
                    errorAndRedirect('该车正被租用中，不可更改');
                }
            }
	        if($this->MCar->update($newObj)){
	            successAndRedirect(l('edit_success'));
	        }else{
	            errorAndRedirect(l('data_fail'));
	        }
	    }
	    $this->assign('obj', $obj);
	    $this->layout();
	}
	function delete($id, $update_time){
	    if(!is_numeric($id) || !is_numeric($update_time)){
	        show_error(l('id_not_null'));
	    }
	    if(!$this->MCar->delete($id, $update_time)){
	        errorAndRedirect(l('data_fail'));
	    }
	    successAndRedirect(l('delete_success'));
	}
}
